package top.lanscarlos.ashcraft.controller;

import com.google.gson.JsonObject;
import com.sun.istack.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.lanscarlos.ashcraft.pojo.Seller;
import top.lanscarlos.ashcraft.service.SellerService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Ashcraft
 * top.lanscarlos.ashcraft.controller
 *
 * @author Lanscarlos
 * @since 2022-05-26 09:47
 */
@RestController
@RequestMapping(value = "/seller")
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class SellerController extends AshController {
    @Autowired
    private SellerService service;

    @RequestMapping("/query")
    public Object query(JsonObject json, String keyword) {
        List<Seller> sellers = service.query(keyword);
//        if (sellers.isEmpty()) {
//            json.addProperty("result", false);
//        } else {
//            json.addProperty("result", true);
//        }
//        json.add("items", toJson(sellers));
        return toJson(sellers);
    }

    @RequestMapping("/register")
    public Object register(
//            JsonObject json,
            HttpServletRequest request,
            @NotNull String name,
            @NotNull String password,
            @Nullable MultipartFile avatar,
            @Nullable String description,
            @NotNull String contact
    ) {
        String avatar_path = handleMultipartFile(request.getServletContext(), avatar);
        Seller seller = service.register(name, password, avatar_path, description, contact);
//        if (seller != null) {
//            json.addProperty("result", true);
//            json.add("user", toJson(seller));
//        } else {
//            json.addProperty("result", false);
//        }
        return toJson(seller);
    }

}
